package com.zhixin.fans.ks.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.blankj.utilcode.util.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.eenie.common.base.BaseApp;
import org.eenie.common.managers.PayManager;
import com.zhixin.fans.ks.R;


/**
 * WXPayEntryActivity 微信支付 包名与类名不能变
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    PayManager mPayManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry_activity);
        mPayManager = ((BaseApp) getApplication()).getApplicationComponent().getPayManager();

        api = WXAPIFactory.createWXAPI(this, "wx96625dc3f9696817");
        api.handleIntent(getIntent(), this);


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);

    }

    @Override
    public void onReq(BaseReq req) {


    }

    @Override
    public void onResp(BaseResp resp) {

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            mPayManager.setValue(resp);
            switch (resp.errCode) {
                case 0:
                    setResult(Activity.RESULT_OK);
                    ToastUtils.showShort("支付成功");
                    break;
                case -1:
                    setResult(1001);
                    ToastUtils.showShort("支付取消");
                    break;
                case -2:
                    setResult(1002);
                    ToastUtils.showShort("支付失败");
                    break;
            }
        }

        finish();
    }
}
